package com.google.code.maven.plugin.http.client;

import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.maven.plugin.logging.Log;

/**
 * 
 * @author 797409
 * 
 */
public class HttpEntityUtils {
	
	public static final Charset DEFAULT_PLATFORM_ENCODING = Charset.defaultCharset();
	
	public static final Pattern CONTENT_TYPE_CHARSET_PATTERN = Pattern.compile("[cC][hH][aA][rR][sS][eE][tT]=");
	
	public static final Pattern CONTENT_TYPE_CHARSET_DELIMITER_PATTERN = Pattern.compile("[|,;\\s]");
	
	public static Charset getEncoding(HttpEntity entity, Log log) {
		try {
			if (entity.getContentEncoding() != null) {
				return Charset.forName(entity.getContentEncoding().getName());
			} else if (entity.getContentType() != null) {
				String type = entity.getContentType().getValue();
				if (type != null) {
					Matcher charsetMatcher = CONTENT_TYPE_CHARSET_PATTERN.matcher(type);
					if (charsetMatcher.find()) {
						Matcher delimiterMatcher = CONTENT_TYPE_CHARSET_DELIMITER_PATTERN.matcher(type);
						String charsetName = null;
						if (delimiterMatcher.find(charsetMatcher.end())) {
							charsetName = type.substring(charsetMatcher.end(), delimiterMatcher.start());
						} else {
							charsetName = type.substring(charsetMatcher.end());
						}
						return Charset.forName(charsetName);
					}
					
				}
			} else {
				log.warn("encoding not defined in content encoding nor in content type");
			}
		} catch (IllegalCharsetNameException icne) {
			log.warn("failed to determine response encoding", icne);
		} catch (UnsupportedCharsetException uce) {
			log.warn("failed to determine response encoding", uce);
		}
		log.warn("back to default platform encoding " + DEFAULT_PLATFORM_ENCODING);
		return DEFAULT_PLATFORM_ENCODING;
	}
}
